package com.qiyi.player.wonder.plugins.videolink.view
{
   import com.iqiyi.components.panelSystem.impls.BasePanel;
   import com.qiyi.player.wonder.common.status.Status;
   import com.qiyi.player.wonder.common.vo.UserInfoVO;
   import videolink.VideoLink;
   import common.CommonBg;
   import flash.display.Loader;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.wonder.plugins.videolink.VideoLinkDef;
   import flash.display.DisplayObjectContainer;
   import com.iqiyi.components.global.GlobalStage;
   import flash.display.SimpleButton;
   import com.qiyi.player.wonder.plugins.videolink.model.VideoLinkInfo;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.events.MouseEvent;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import com.qiyi.player.base.logging.Log;
   
   public class VideoLinkView extends BasePanel
   {
      
      public function VideoLinkView(param1:DisplayObjectContainer, param2:Status, param3:UserInfoVO) {
         this._log = Log.getLogger(NAME);
         super(NAME,param1);
         this._status = param2;
         this._userInfoVO = param3;
         this.initUI();
      }
      
      public static const NAME:String = "com.qiyi.player.wonder.plugins.videolink.view.VideoLinkView";
      
      private static const CONST_DISTANCE:uint = 70;
      
      private var _status:Status;
      
      private var _userInfoVO:UserInfoVO;
      
      private var _videoLink:VideoLink;
      
      private var _commanBg:CommonBg;
      
      private var _loader:Loader;
      
      private var _log:ILogger;
      
      public function onUserInfoChanged(param1:UserInfoVO) : void {
         this._userInfoVO = param1;
      }
      
      public function onAddStatus(param1:int) : void {
         this._status.addStatus(param1);
         switch(param1)
         {
            case VideoLinkDef.STATUS_OPEN:
               this.open();
               break;
         }
      }
      
      public function onRemoveStatus(param1:int) : void {
         this._status.removeStatus(param1);
         switch(param1)
         {
            case VideoLinkDef.STATUS_OPEN:
               this.close();
               break;
         }
      }
      
      override public function open(param1:DisplayObjectContainer = null) : void {
         if(!isOnStage)
         {
            super.open(param1);
            dispatchEvent(new VideoLinkEvent(VideoLinkEvent.Evt_Open));
         }
      }
      
      override public function close() : void {
         if(isOnStage)
         {
            super.close();
            dispatchEvent(new VideoLinkEvent(VideoLinkEvent.Evt_Close));
         }
      }
      
      public function onResize(param1:int, param2:int) : void {
         this._commanBg.y = this._videoLink.y = GlobalStage.stage.stageHeight - this._videoLink.height - CONST_DISTANCE;
      }
      
      public function get watchVideoBtn() : SimpleButton {
         return this._videoLink.watchVideoBtn;
      }
      
      public function initPanel(param1:VideoLinkInfo) : void {
         this._videoLink.tfVideoLinkDec.text = param1.title;
         this.loaderPNG(param1.picUrl);
         switch(param1.btnType)
         {
            case VideoLinkDef.BTN_TYPE_PLAY:
               this._videoLink.tfBtnType.text = "立即观看";
               break;
            case VideoLinkDef.BTN_TYPE_DOWNLOAD:
               this._videoLink.tfBtnType.text = "立即下载";
               break;
            case VideoLinkDef.BTN_TYPE_INSERT:
               this._videoLink.tfBtnType.text = "立即进入";
               break;
         }
         this._log.debug("show videolink bt = " + param1.startTime + ", et = " + param1.endTime);
      }
      
      private function initUI() : void {
         this._commanBg = new CommonBg();
         this._commanBg.width = 290;
         this._commanBg.height = 70;
         addChild(this._commanBg);
         this._videoLink = new VideoLink();
         this._videoLink.x = 2;
         this._videoLink.tfBtnType.selectable = this._videoLink.tfBtnType.mouseEnabled = false;
         this._videoLink.tfVideoLinkDec.defaultTextFormat = this._videoLink.tfBtnType.defaultTextFormat = FastCreator.createTextFormat("微软雅黑");
         this._commanBg.y = this._videoLink.y = GlobalStage.stage.stageHeight - this._videoLink.height - CONST_DISTANCE;
         this._videoLink.closeBtn.addEventListener(MouseEvent.CLICK,this.onCloseBtnClick);
         addChild(this._videoLink);
      }
      
      private function loaderPNG(param1:String) : void {
         this.destroyUpLoader();
         this._loader = new Loader();
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
         this._loader.load(new URLRequest(param1));
      }
      
      private function onComplete(param1:Event) : void {
         if(isOnStage)
         {
            this._videoLink.addChild(this._loader);
         }
      }
      
      private function onIOError(param1:Event) : void {
      }
      
      private function destroyUpLoader() : void {
         if(this._loader == null)
         {
            return;
         }
         if(this._loader.parent)
         {
            this._videoLink.removeChild(this._loader);
         }
         this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onComplete);
         this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
         this._loader = null;
      }
      
      private function onCloseBtnClick(param1:MouseEvent) : void {
         this.close();
      }
   }
}
